//
//  CAAlertView.m
//  cascade-realm
//
//  Created by QSC on 19/5/24.
//

#import "CAAlertView.h"
@interface CAAlertView()
@property(nonatomic,strong)UIView * contentView;
@property(nonatomic,strong)NSArray * ActionSheets;
@property(nonatomic,strong)UIView * SheetView;
@property (nonatomic,copy)void(^SheetBlock)(NSString *action);
@end
@implementation CAAlertView

+(CAAlertView *)ShowAlertWithContent:(UIView *)contentView
{
    
    
    CAAlertView * alert = [CAAlertView new];
    alert.frame = [UIScreen mainScreen].bounds;
    alert.contentView = contentView;
    
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    
    return alert;
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        
        
    }
    return self;
}

- (void)setIsDismissible:(BOOL)isDismissible{
    _isDismissible = isDismissible;
    if (isDismissible) {
        WeakSelf
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf removeFromSuperview];
        }];
        
        [self addGestureRecognizer:  tap];
    }
}

- (void)setContentView:(UIView *)contentView{
    _contentView = contentView;
    [self addSubview:contentView];
    [contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.center.equalTo(self);
        make.width.mas_equalTo(ScreenW * 0.8);
        
    }];
}


+ (CAAlertView *)ShowAlertActionSheet:(NSArray *)titles actionBlock:(void(^)(NSString *))block{
    
    CAAlertView * alert = [CAAlertView new];
    alert.frame = [UIScreen mainScreen].bounds;
    alert.ActionSheets = titles;
    alert.SheetBlock = block;
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    
    return alert;
}


- (void)setActionSheets:(NSArray *)ActionSheets{
    _ActionSheets = ActionSheets;
    
    
    
    UIView * contentView = [UIView new];
    contentView.backgroundColor = [UIColor whiteColor];
    contentView.layer.cornerRadius = 12;
    
    [self addSubview:contentView];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.mas_bottom).offset(10);
    }];
    
    UIButton * temp;
    for (int i = 0; i < ActionSheets.count; i++) {
        NSString * title = ActionSheets[i];
        UIButton * btn = [UIButton ButtonWithFont:Font(16) color:Hex(0x142220) text:title];
        [contentView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.top.equalTo(contentView);
            }else{
                make.top.equalTo(temp.mas_bottom);
            }
            make.left.right.equalTo(contentView);
            make.height.mas_equalTo(50);
        }];
        UIView *line = [UIView new];
        line.backgroundColor = Hex(0xEDEDED);
        [btn addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(btn).inset(20);
            make.height.mas_equalTo(1);
            make.bottom.equalTo(btn);
        }];
        temp = btn;
        [btn addTarget:self action:@selector(SheetAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIButton * btn = [UIButton ButtonWithFont:BoldFont(16) color:[UIColor whiteColor] text:@"Cancel"];
    [contentView addSubview:btn];
    btn.layer.cornerRadius = 22;
    btn.clipsToBounds = YES;
    [Tool colorGradientLayerForView:btn frame:CGRectMake(0, 0, 180, 44) colors:@[Hex(0xB3E600),Hex(0xD4E600)] locations:@[@0.0,@1.0]];
    [btn addTarget:self action:@selector(exitSheetView) forControlEvents:UIControlEventTouchUpInside];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contentView);
        make.top.equalTo(temp.mas_bottom).offset(30);
        make.bottom.equalTo(contentView).offset(-20 - kBottomSafeHeight);
        make.width.mas_equalTo(180);
        make.height.mas_equalTo(44);
    }];
    self.SheetView = contentView;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showSheetView];
    });
   
   
    
}

-(void)showSheetView{
    
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.SheetView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
          
            make.bottom.equalTo(self).offset(10);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
      
    }];
}

-(void)exitSheetView{
    [UIView animateWithDuration:0.3 animations:^{
        [self.SheetView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.mas_bottom).offset(10);
        }];
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
   
}

- (void)SheetAction:(UIButton *)btn{
    
    [self exitSheetView];
    !self.SheetBlock?:self.SheetBlock(btn.titleLabel.text);
}

@end
